package com.demo.service.impl;

import com.demo.dto.UserDto;
import com.demo.mapper.ComployeeMapper;
import com.demo.mapper.UserMapper;
import com.demo.pojo.Comployee;
import com.demo.pojo.User;
import com.demo.service.ComployeeService;
import com.demo.service.UserService;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/07 19:12
 * 描述：
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ComployeeMapper comployeeMapper;

    @Override
    public Map<String, Object> login(User user) {
        Map<String, Object> map = new HashMap<>();
        User username = userMapper.selectByPrimaryKey(user.getUsername());
        if (username == null) {
            map.put("code", 1);
            map.put("message", "账号不存在！");
            return map;
        }
        if (!username.getState()) {
            map.put("code", 3);
            map.put("message", "账号已被停用！");
            return map;
        }
        user = userMapper.select(user);
        if (user == null) {
            map.put("code", 2);
            map.put("message", "密码错误！");
            return map;
        }
        map.put("data", user);
        map.put("code", 0);
        map.put("message", "登录成功！");
        return map;
    }

    @Override
    public UserDto selectDto(String username) {
        User user = new User();
        user.setUsername(username);
        return userMapper.selectDto(user);
    }

    @Override
    public User select(User user) {
        return userMapper.select(user);
    }

    @Override
    public Map<String, Object> updatePassword(String username, String password, String newPassword) {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setUsername(username);
        user.setPwd(password);
        user = userMapper.select(user);
        if (user == null) {
            map.put("code", 1);
            map.put("message", "原始密码错误！");
            return map;
        }
        user.setPwd(newPassword);
        userMapper.updateByPrimaryKeySelective(user);
        map.put("code", 0);
        map.put("message", "修改密码成功！");
        return map;
    }

    @Override
    public int add(User user) {
        String comployeeNo = user.getComployeeNo();
        Comployee comployee = comployeeMapper.selectByPrimaryKey(comployeeNo);
        if (comployee == null) {
            return -1;
        }
        String username = user.getUsername();
        if ("".equals(user.getPwd()) || user.getPwd() == null) {
            user.setPwd(username.substring(username.length() - 6));
        }
        user.setState(true);
        if (userMapper.selectByPrimaryKey(username) != null) {
            return -2;
        }
        return userMapper.insertSelective(user);
    }

    @Override
    public int del(String id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            return -3;
        }
        if (user.getCompetenceId() > 0) {
            return -2;
        }
        if (user.getComployeeNo() != null) {
            return -1;
        }
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(User userUpdate) {
        User user = userMapper.selectByPrimaryKey(userUpdate.getUsername());
        if (user == null) {
            return -3;
        }
        if (user.getCompetenceId() > 0) {
            return -2;
        }
        String comployeeNo = userUpdate.getComployeeNo();
        if (!("".equals(comployeeNo) || comployeeNo == null)) {
            Comployee comployee = comployeeMapper.selectByPrimaryKey(comployeeNo);
            if (comployee == null) {
                return -1;
            }
        }
        if ("".equals(userUpdate.getPwd())) {
            userUpdate.setPwd(null);
        }
        return userMapper.updateByPrimaryKeySelective(userUpdate);
    }

    @Override
    public Page<User> page(User user, int pageNo, int pageSize) {
        Page<User> page = new Page<>();
        page.setPageSize(pageSize);
        int count = userMapper.pageCount(user);
        page.setPageTotalCount(count);
        int total = count / pageSize;
        if (count % pageSize > 0) {
            total++;
        }
        page.setPageTotal(total);
        page.setPageNo(pageNo);
        List<User> users = userMapper.pageByCond(user, (pageNo - 1) * pageSize, pageSize);
        page.setItems(users);
        return page;
    }
}
